const {handleCount,handleTime} = require("../../utils/index")
const {getPlaylistDetail} = require("../../models/playlistDetail")
const audio = wx.getBackgroundAudioManager();
Page({
  data: {
    arr:"",
    res:"",
    playStates:{}
  },
  onLoad: function (options) {
    var {id} = options
    getPlaylistDetail(id).then(res=>{
      var arr = res.data.playlist
      arr.playCount = handleCount(arr.playCount)
      arr.subscribedCount = handleCount(arr.subscribedCount)
      arr.commentCount = handleCount(arr.commentCount)
      arr.shareCount = handleCount(arr.shareCount)
      var res = arr.tracks
      res.forEach(item=>{
        item.dt = handleTime(item.dt)
      })
      this.setData({
        arr,
        res
      })
    })
  },
  onShow(){
    var states = wx.getStorageSync('states')
    this.setData({
      playStates:states
    })
  },
  handlePlay(event){
    let id = event.currentTarget.dataset.aid;
    let playStates = wx.getStorageSync('states');

    playStates[id] = !playStates[id]
    for(let key in playStates){
      if(key != id){
        playStates[key] = false
      }
    }
    this.setData({
      playStates
    })
    wx.setStorageSync('states', playStates)
    let {url,name} = event.currentTarget.dataset
    wx.setStorageSync('url', url)
    wx.setStorageSync('name', name)

    if(playStates[id]){
      wx.navigateTo({
        url: `/pages/playSong/playSong?id=${id}`
      });
    }else{
        audio.pause()
    }
  }
})